package cn.z2huo.knowledge.constant;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * @author z2huo
 */
@Getter
@RequiredArgsConstructor
enum ErrorCodeEnum {

    SUCCESS("00000", "成功"),

    /**
     * 用户端错误
     */
    CLIENT_ERROR("A0001", "用户端错误"),

    /**
     * 服务端错误
     */
    SERVER_ERROR("B0001", "系统执行出错"),

    /**
     * 调用第三方服务出错
     */
    OTHER_SERVER_ERROR("C0001", "调用第三方服务出错"),

    /**
     * 自定义错误码
     */
    CUSTOM_ERROR("Y", "自定义错误"),

    /**
     * 未知错误
     */
    UNKNOWN("Z0000", "未知错误")

    ;

    private final String code;

    private final String message;

    private static final Map<String, ErrorCodeEnum> codeMap = new HashMap<>();

    static {
        Arrays.stream(ErrorCodeEnum.values()).forEach(x -> codeMap.put(x.getCode(), x));
    }

    public static ErrorCodeEnum fromCode(String code) {
        return codeMap.get(code);
    }

}
